﻿using pTools.cMethods;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace pTools.ChildWin
{
    public partial class SelectCommonData : Form
    {
        string dType = "";
        cMethods.DBHelper db;
        public SelectCommonData(string dataType)
        {
            InitializeComponent();
            dType = dataType;
            db = new cMethods.DBHelper();
        }

        private void SelectCommonData_Load(object sender, EventArgs e)
        {
            try
            {
                this.Text = "数据选择";
                loadData();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        private void loadData()
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("select cCode,cName ");
            sb.Append("from fun_CommonData_Info where isdel=0 and status='Y'");
            sb.Append(" and cType='" + dType + "'");
            if (!string.IsNullOrWhiteSpace(tbCode.Text))
            {
                sb.Append(" and cCode like '%" + tbCode.Text.Trim() + "%' ");
            }
            if (!string.IsNullOrWhiteSpace(tbName.Text))
            {
                sb.Append(" and cName like '%" + tbName.Text.Trim() + "%' ");
            }
            sb.Append(" order by id asc");
            var table = db.getData(sb.ToString(), "");
            this.dvList.DataSource = table;
            this.dvList.AutoGenerateColumns = false;
        }

        private void dvList_DoubleClick(object sender, EventArgs e)
        {
            try
            {
                if (dvList.RowCount > 0)
                {
                    CommonMethods.singleValue = this.dvList.CurrentRow.Cells["cCode"].Value.ToString();
                    CommonMethods.singleText= this.dvList.CurrentRow.Cells["cName"].Value.ToString();
                    this.Close();
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void btnQuery_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            try
            {
                loadData();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void tbCode_KeyDown(object sender, KeyEventArgs e)
        {
            try
            {
                if (!string.IsNullOrWhiteSpace(tbCode.Text))
                {
                    if (e.KeyCode == Keys.Enter)
                    {
                        loadData();
                    }
                }
              
                
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void tbName_KeyDown(object sender, KeyEventArgs e)
        {
            try
            {
                if (!string.IsNullOrWhiteSpace(tbName.Text))
                {
                    if (e.KeyCode == Keys.Enter)
                    {
                        loadData();
                    }
                }


            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
    }
}
